
#include  <msp430x54xA.h>
#include  "Drivers.h"
#include  "..\ESS_CommonDefine.h"

/*
    MosFET Control configuration 
    P9.7 CHG control
    P9.6 DSG control
*/

void InitMosControl(){

  // Set up pins used by IO unit
  MOSFET_PORT_SEL &= ~(MOSFET_CHG + MOSFET_DSG);
  MOSFET_PORT_DIR |= MOSFET_CHG + MOSFET_DSG;
  //MOSFET_PORT_OUT &= ~(MOSFET_CHG + MOSFET_DSG);
  
  //DSG MOS inverse
  MOSFET_PORT_OUT |= MOSFET_DSG;
  //CHG MOS inverse
  MOSFET_PORT_OUT |= MOSFET_CHG;
  ///////////////////////////////////////////////////
  G_Device_Action_Controls &= ~(CHG_MOSFET_STATUS);
  G_Device_Action_Controls &= ~(DSG_MOSFET_STATUS);
  
}

void setMosCHG(unsigned char enable){
  if(enable){
    //DeviceOn
//    if(G_Device_Action_Controls & CHG_MOSFET_STATUS){
//      return;
//    }
    MOSFET_PORT_OUT &= ~(MOSFET_CHG);
    G_Device_Action_Controls |= (CHG_MOSFET_STATUS);
  }else{
    //DeviceOff
//    if((G_Device_Action_Controls & CHG_MOSFET_STATUS)==0){
//      return;
//    }
    MOSFET_PORT_OUT |= (MOSFET_CHG);
    G_Device_Action_Controls &= ~(CHG_MOSFET_STATUS);
  }
//  if(G_Device_Action_Controls & (CHG_MOSFET_STATUS)){
//    P1OUT |= BIT6;
//  }else{
//    P1OUT &= ~BIT6;
//  }
}

void setMosDSG(unsigned char enable){
  if(enable){
    //DeviceOn
//    if(G_Device_Action_Controls & DSG_MOSFET_STATUS){
//      return;
//    }
    MOSFET_PORT_OUT &= ~(MOSFET_DSG);
    G_Device_Action_Controls |= (DSG_MOSFET_STATUS);
  }else{
    //DeviceOff
//    if((G_Device_Action_Controls & DSG_MOSFET_STATUS)==0){
//      return;
//    }
    MOSFET_PORT_OUT |= (MOSFET_DSG);
    G_Device_Action_Controls &= ~(DSG_MOSFET_STATUS);
  }
//  if(G_Device_Action_Controls & (DSG_MOSFET_STATUS)){
//    P1OUT |= BIT5;
//  }else{
//    P1OUT &= ~BIT5;
//  }
  
}
/*
void setMosCHG(char enable){
  if(enable){
    MOSFET_PORT_OUT |= (MOSFET_CHG);
    CHGflag = enable;
  }else{
    MOSFET_PORT_OUT &= ~(MOSFET_CHG);
    CHGflag = enable;
  }
}
*/
/*
void setMosDSG(char enable){
  if(enable){
    MOSFET_PORT_OUT |= (MOSFET_DSG);
    DSGflag = enable;
  }else{
    MOSFET_PORT_OUT &= ~(MOSFET_DSG);
    DSGflag = enable;
  }
}
*/


unsigned char getMosCHG(){
  if(G_Device_Action_Controls & CHG_MOSFET_STATUS){
    return DeviceOn;
  }else{
    return DeviceOff;
  }
}
unsigned char getMosDSG(){
  if(G_Device_Action_Controls & DSG_MOSFET_STATUS){
    return DeviceOn;
  }else{
    return DeviceOff;
  }
}
